﻿@using lwzlz.Core;
@{
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
    var deep1List = ViewData["deep1List"] as List<Place>;
    var deep2List = ViewData["deep2List"] as List<Place>;
    var deep3List = ViewData["deep3List"] as List<Place>;
}

@model MemberOutput

<form class="layui-form layui-form-pane">
    @Html.AntiForgeryToken()
    <input type="hidden" value="@Model.Id" name="Id" />
    <div class="layui-form-item" style="padding-top:10px;">
        <div class="layui-col-md4">
            <label class="layui-form-label">省份</label>
            <div class="layui-input-block">
                <select name="ProvinceCode" lay-search lay-filter="place" id="ProvinceCode" lay-verify="required">
                    <option value=""></option>
                    @{
                        foreach (var item in deep1List)
                        {
                            <option value="@item.Code" @(Model.ProvinceCode == item.Code ? "selected" : "")>@item.Name</option>

                        }
                    }
                </select>
            </div>
        </div>
        <div class="layui-col-md4">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-block">
                <select name="CityCode" lay-search lay-filter="place" id="CityCode" lay-verify="required">
                    <option value=""></option>
                    @{
                        foreach (var item in deep2List.FindAll(p => p.ParentID == Model.ProvinceCode))
                        {
                            <option value="@item.Code" @(Model.CityCode == item.Code ? "selected" : "")>@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
        <div class="layui-col-md4">
            <label class="layui-form-label">地区</label>
            <div class="layui-input-block">
                <select name="DistrictCode" lay-search lay-filter="place" id="DistrictCode" lay-verify="required">
                    <option value=""></option>
                    @{
                        foreach (var item in deep3List.FindAll(p => p.ParentID == Model.CityCode))
                        {
                            <option value="@item.Code" @(Model.DistrictCode == item.Code ? "selected" : "")>@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
    </div>
    <input type="hidden" name="ProvinceName" value="@Model.ProvinceName" id="ProvinceName" />
    <input type="hidden" name="CityName" value="@Model.CityName" id="CityName" />
    <input type="hidden" name="DistrictName" value="@Model.DistrictName" id="DistrictName" />
    <div class="layui-form-item">
        <label class="layui-form-label">学校名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="SchoolName" value="@Model.SchoolName" lay-verify="required" required>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">账号/手机</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="UserName" value="@Model.UserName" lay-verify="required" required>
        </div>
    </div>
    @if (Model.Id == 0)
    {
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" class="layui-input" name="PassWord" value="@Model.PassWord" lay-verify="required" required>
            </div>
        </div>
    }
    <div class="layui-form-item">
        <label class="layui-form-label">校长名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="RealName" value="@Model.RealName" lay-verify="required" required>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">校长电话</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="Phone" value="@Model.Phone" lay-verify="required" required>
        </div>
    </div>
    @*<div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="Email" value="@Model.Email">
            </div>
        </div>*@
    <div class="layui-form-item">
        <label class="layui-form-label">加盟类型</label>
        <div class="layui-input-block">
            <select name="JoinType" lay-verify="required" lay-search="">
                <option value="">请选择</option>
                <option value="1" @(Model.JoinType == 1 ? "selected=selected" : "")>单店</option>
                <option value="2" @(Model.JoinType == 2 ? "selected=selected" : "")>独家</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">加盟时间</label>
        <div class="layui-input-block">
            <input type="text" name="JoinTime" value="@(Model.JoinTime!=null?  Convert.ToDateTime(Model.JoinTime).ToString("yyyy-MM-dd"):"")" class="layui-input" id="JoinTime" lay-verify="required" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="Status" lay-verify="required" lay-search="">
                <option value="">请选择</option>
                <option value="1" @(Model.Status == 1 || Model.Status == 0 ? "selected=selected" : "")>启用</option>
                <option value="2" @(Model.Status == 2 ? "selected=selected" : "")>禁用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">最后登录时间</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="LastLoginTime" value="@Model.LastLoginTime" readonly>
        </div>
    </div>
    <div class="layui-form-item" align="center">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="Save">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
    </div>
</form>

@section scripts{
    <script type="text/javascript">
        var $, layer;
        layui.use(['form', 'laydate'], function () {
            var form = layui.form;
            var laydate = layui.laydate;
            $ = layui.jquery;
            layer = parent.layer === undefined ? layui.layer : parent.layer;

            //日期时间范围
            laydate.render({
                elem: '#JoinTime'
            });

            // 监听关闭
            $('#btnCancel').on('click', function () {
                layer_close();
            });

            //地区三级联动
            form.on('select(place)', function (data) {
                var urls = '@Url.Action("GetPlace", "Place")' + '?Pid=' + data.value;
                var name = $(data.elem).attr("name");
                if (name == "ProvinceCode") {
                    getJSON(urls, $("#CityCode"));
                    getJSON(urls, $("#DistrictCode"));
                    var ee = $("#ProvinceCode").find("option:selected").text();
                    $("#ProvinceName").val(ee);
                }
                if (name == "CityCode") {
                    getJSON(urls, $("#DistrictCode"));
                    var ee = $("#CityCode").find("option:selected").text();
                    $("#CityName").val(ee);
                }
                if (name == "DistrictCode") {
                    var ee = $("#DistrictCode").find("option:selected").text();
                    $("#DistrictName").val(ee);
                }
                //选中内容保存到隐藏域
                $(data.elem).siblings("input:hidden").val($(data.othis).find("dd[class='layui-this']").html());
                console.log($(data.elem).siblings("input:hidden").val());
            });

            function getJSON(urls, even) {
                $.post(urls, function (json) {
                    var name = even.find("select").attr("name");
                    var select = '<select id="' + name + '" name="' + name + '" lay-filter="state" lay-verify="required">';
                    select += '<option value="">请选择</option>';
                    $(json).each(function () {
                        select += '<option value="' + this.Code + '">' + this.Name + '</option>';
                    });
                    select += '</select>';
                    even.html(select);
                    form.render('select');
                });
            };

            var postUrl = '@(Url.Action(Model.Id > 0 ? "Edit" : "Add", "Member"))';

            // 监听提交
            form.on('submit(Save)', function (data) {
                var loading = layer.load();
                $.post(postUrl, data.field,
                    function (result) {
                        layer.close(loading);
                        if (result.Success) {
                            layer.msg(result.Message, { icon: 1, time: 2000 });
                            parent.reload();
                            layer_close();
                        } else {
                            layer.alert(result.Message, { icon: 2 });
                        }
                    });
                return false;
            })

        });
    </script>
}



